#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

#include <string.h>

#include <pthread.h>
#include "mydev_app.h"

void dev_read(int fd)
{
    int i;
    int ret;
    char buf[MEM_SIZE];
    ret = read(fd, buf, sizeof(buf));
    printf("%d\n", ret);
    for (i = 0; i < 100; ++i) {
        printf("%x ", buf[i]);
    }
    printf("\n");
}

int dev_ioctl(int fd)
{
    return  ioctl(fd, GM_IOCTL_CMD_CLEAR, 0);
}
#define PAGE_SIZE (4*1024)
#define BUF_SIZE (16*PAGE_SIZE)

#define MEM_SIZE BUF_SIZE

void dev_map(int fd)
{
    int i;
	char *mmap_mem = NULL; 
    mmap_mem = (char *) mmap (NULL, MEM_SIZE,
                              PROT_READ | PROT_WRITE, MAP_SHARED,
                              fd, 0);
    if(!mmap_mem) printf("err when map.\n");
    for (i = 0; i < MEM_SIZE; ++i) {
        mmap_mem[i] = i;
    }
    // 理论上，应该是获取好大小以后，再进行map
	/* 释放映射区 */
	munmap(mmap_mem, MEM_SIZE);
}

int main(int argc, char *argv[])
{
    int fd;
    fd = open("/dev/mydev", O_RDWR);

    dev_map(fd);

    dev_read(fd);
    dev_ioctl(fd);
    dev_read(fd);

    return 0;
}
